<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div class="alert alert-primary" title="题库" role="alert">题库<el-button type="primary" @click="add" plain style="float: right;background:#2F81F1;color:#fff;line-height: 5px;">添加测试题</el-button></div>
        
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        
        <el-dialog :title="message" :visible.sync="dialogVisible" width="350">
          <el-form :model="form" :rules="rules" ref="ruleForm">
            <el-form-item label="标题" prop="name" label-width="150">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="简介" label-width="150">
              <el-input v-model="form.info"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addteacheritembank">确 定</el-button>
          </span>
        </el-dialog>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table :data="getTeacheritembank.slice((currentPage-1)*pagesize,currentPage*pagesize)" style="width: 100%;height: -webkit-fill-available;">
          <el-table-column prop="name" label="标题"></el-table-column>
          <el-table-column prop="info" label="介绍"></el-table-column>
          <el-table-column prop="created_at" label="创建日期"></el-table-column>
          <el-table-column prop="updated_at" label="更新日期"></el-table-column>
          <el-table-column fixed="right" label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="text" size="small" style="background: #409EFF;width: 35px;color: snow;">
                <router-link class="shanchu"
                  :to="{ name:'teacheritembankid', params: { ID: scope.row.ID }, query: scope.row }"
                >查看</router-link>
              </el-button>
              <el-button type="text" size="small" style="background: #409EFF;width: 35px;color: snow;" @click="update(scope.row)">编辑</el-button>
              <el-button type="text" size="small" style="background: #409EFF;width: 35px;color: snow;" @click="del(scope.row.ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <div class="row col-sm-5 block" style="float: right;">
      <el-pagination
      v-show="this.getTeacheritembank.length>20"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[20, 50, 100, this.getTeacheritembank.length]"
        :page-size="20"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.getTeacheritembank.length"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import {
  asynccreateteacheritembank,
  asyncupdateteacheritembank,
  asyncdelteacheritembank
} from "../../../plugins/api.js";
export default {
  name: "itembank",
  computed: mapGetters(["getTeacheritembank"]),
  data() {
    return {
      message: "添加",
      dialogVisible: false,
      form: {
        name: "",
        info: ""
      },
       currentPage: 1,
      pagesize: 20,
      rules: {
        name: [
          { required: true, message: "请输入测试题标题", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ]
      },
      tableData: []
    };
  },
  watch: {
     currentPage(v) {
      console.log(v);
    },
    pagesize(v) {
      console.log(v);
    },
  },
  async mounted() {},
  methods: {
     handleSizeChange(val) {
      this.pagesize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    async addteacheritembank() {
      this.$refs["ruleForm"].validate(async valid => {
        if (valid) {
          if (this.form.ID) {
            await asyncupdateteacheritembank(this.form);
          } else {
            await asynccreateteacheritembank(this.form);
          }
          await this.$store.dispatch("actionTeacherbyiditembank");
          this.dialogVisible = false;
          this.$message({
            message: `${this.message}成功`,
            type: "success"
          });
        } else {
          return false;
        }
      });
    },
    async del(ID) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await asyncdelteacheritembank({ ID });
          this.$message({
            type: "success",
            message: "删除成功!"
          });
           await this.$store.dispatch("actionTeacherbyiditembank");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    add() {
      this.dialogVisible = true;
      this.message = "添加";
      this.form = {
        name: "",
        info: ""
      };
    },
    update(data) {
      this.dialogVisible = true;
      this.message = "修改";
      this.form = JSON.parse(JSON.stringify(data));
    }
  }
};
</script>

<style lang="scss" scoped>
.rectangle-5 {
  background-color: #ffffff;
  box-shadow: 1px 2px 6px 0px rgba(139, 157, 175, 0.1);
  padding: 26px;
}

h5 {
  margin-right: 45px;
}
.alert-primary{
      color:black; 
    background-color:#eee;
    border-color:#eee;
    background: #fff;
}
.shanchu:hover{
  color:#ffffff !important;
  text-decoration: #fff;
}
.shanchu{
  color:#ffffff !important;
}
</style>


